// pages/home/business/business.js
let app = getApp();
let testAddr = app.globalData.testAddr;
let imgurl = app.globalData.imgurl;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    login: [{
        id: "1",
        logname: "求购"
      },
      {
        id: "2",
        logname: "供应"
      },
      {
        id: "3",
        logname: "朋友圈"
      }
    ],
    currentTabsIndex: 0,
    clicklike: false, //点赞状态 默认没有点击
    pageIndex: 1,
    allcirclelist: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    that.getarticlemessig()
  },
  onTabsItemTap: function(event) {
    var that = this
    var index = event.currentTarget.dataset.index;
    this.setData({
      currentTabsIndex: index
    });
    that.setData({
      pageIndex: 1,
      allcirclelist: []
    })
    that.getarticlemessig()
  },
  //  获取求购、供应、朋友圈信息
  getarticlemessig: function() {
    var that = this
    var pageIndex = that.data.pageIndex
    var anonymous = that.data.currentTabsIndex + 1
    var guestID = wx.getStorageSync("list").guestid;
    var allcirclelist = that.data.allcirclelist
    that.setData({
      guestID: guestID,
      anonymous: anonymous
    })
    app.util.request({
      url: testAddr + "article/getRealArticle?pageIndex=" +
        pageIndex + '&anonymous=' + anonymous + '&guestID=' + guestID,
      method: "POST",
      data: {},
      success: function(res) {
        var circlelist = res.data
        if (circlelist.length != 0) {
          that.setData({
            pageIndex: pageIndex + 1
          })
          for (var i = 0; i < circlelist.length; i++) {
            var time = circlelist[i].createtime
            if (time) {
              circlelist[i].createtime = time.slice(0, 16)
            }
            if (circlelist[i].articleImgs) {
              var imgArr = []
              for (var k = 0; k < circlelist[i].articleImgs.length; k++) {
                imgArr.push(imgurl + 'article/' + circlelist[i].articleImgs[k].imgurl)
              }
              circlelist[i].imgArr = imgArr
            }
          }
          allcirclelist = allcirclelist.concat(circlelist)
        }
        console.log(allcirclelist)
        that.setData({
          allcirclelist: allcirclelist
        })
      }
    })
  },
  // 预览图片
  viewphoto: function(e) {
    var that = this
    var imgsrc = e.currentTarget.dataset.src
    var index = e.currentTarget.id
    var urls = that.data.allcirclelist[index].imgArr
    wx.previewImage({
      current: imgsrc,
      urls: urls
    })
  },
  // 点赞按钮
  clicklikenum: function(e) {
    var that = this
    var guestID = that.data.guestID
    var articleId = e.currentTarget.id
    var likenum = e.currentTarget.dataset.likenum
    var clicklike = that.data.clicklike
    console.log(guestID)
    app.util.request({
      url: testAddr + "article/updateArticle?guestID=" + guestID + "&articleId=" + articleId + "&likenum=" + likenum,
      method: "POST",
      data: {

      },
      success: function(res) {
        console.log(res)
        that.setData({
          clicklike: true
        })
        console.log(clicklike)
      }
    })

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this
    // var companyID = that.data.companyID
    that.getarticlemessig()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})